<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Show\Type;
use App\Admin\Repositories\Ad;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AdController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Ad(), function (Grid $grid) {

            // $grid->scrollbarX();
            $grid->withBorder();
            $grid->addTableClass(['table-text-center']);
            $grid->disableViewButton();

            /**
             * 按钮控制.
             */
            // @permissions
            if (!Admin::user()->can('ads-create')) {
                $grid->disableCreateButton();
                // $grid->disableQuickCreateButton();
            }
            // @permissions
            if (!Admin::user()->can('ads-update')) {
                $grid->disableEditButton();
                // $grid->disableQuickEditButton();
            }
            // @permissions
            if (!Admin::user()->can('ads-delete')) {
                $grid->disableDeleteButton();
            }

            $grid->column('id')->sortable();
            // $grid->column('title');
            $grid->column('img')->customImage();
            $grid->column('type')->using(Ad::getAdType());
            if (Admin::user()->can('ads-status')) {
                $grid->column('status', '启用状态')->switch();
            }
            // $grid->column('is_jump')->using(Ad::isJump())->badge([1 => 'success', 0 => 'danger']);
            // $grid->column('href')->link();
            // $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('type')->select(Ad::getAdType());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Ad(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('img');
            $show->field('type');
            $show->field('status');
            $show->field('is_jump');
            $show->field('href')->as(function ($actions) {
                return '<a href="your_url" class="btn btn-sm btn-danger">你的按钮文本</a>';
            });
            $show->field('created_at');
            $show->field('updated_at');
            // 添加按钮到详情页面

            // $show->tools(function (Show\Tools $tools) {
            //     $tools->append(new Type());
            // });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Ad(), function (Form $form) {
            $form->disableViewButton();
            $form->disableViewCheck();
            $form->display('id');
            //
            $form->select('type')->options(Ad::getAdType())->rules('required', ['required' => '请补充完整该项']);
            $form->image('img')
                ->saveAsString()
                ->saveFullUrl()
                ->autoUpload()
                ->rules('required', ['required' => '请上传广告图'])
                ->saving(function ($path) {
                    return httpToHttps($path);
                });
            $form->editor('title', '广告语');
            $form->radio('is_jump', '是否跳转')->options(Ad::isJump())->default(0)
                ->when(1, function (Form $form) {
                    $form->url('href', '跳转链接')
                        ->rules('required_if:is_jump,1', ['required_if' => '请补充完整该项']);
                });

            // $form->button('填充数据');
            $form->radio('status', '状态')->options(['1' => '启用', '0' => '停用'])->default('1');
            // $form->button('填充数据');
            // $form->display('created_at');
            // 自定义按钮列
            // $form->button('填充数据')->form()->model();
        });
    }
}
